package com.life.muyu.schedule;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.life.muyu.mapper.MailSendRecordsMapper;
import com.life.muyu.mapper.MailSubscriberMapper;
import com.life.muyu.mapper.NewsMapper;
import com.life.muyu.model.MailSendRecord;
import com.life.muyu.model.MailSubscriber;
import com.life.muyu.model.News;
import com.life.muyu.service.MailService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import java.util.stream.Collectors;

/**
 * @Author zyb
 * @Date 2020-04-16 10:03 PM
 **/
@Component
public class MailTask {
    @Autowired
    private MailService mailService;

    @Autowired
    private NewsMapper newsMapper;

    @Autowired
    private MailSendRecordsMapper recordsMapper;

    @Autowired
    private MailSubscriberMapper mailSubscriberMapper;

    @Value("${localhost.addr}")
    private String localhost;

    @Value("${spring.mail.nickname}")
    private String nickName;

    @Value("${spring.mail.username}")
    private String fromMailAddress;

//    fixRate距离上一次打印多少秒后执行一次
//    @Scheduled(fixedRate = 1000*10)
//    public void taskTest(){
//        System.out.println("每隔10秒打印一次");
//    }

    @Scheduled(cron = "00 30 17 ? * * ")
    @Transactional
    public void sendMail(){
        List<News> news = newsMapper.selectList(new QueryWrapper<News>().orderByAsc("news_date"));
        List<MailSubscriber> subscribers = mailSubscriberMapper.selectList(null);
        StringBuilder content = new StringBuilder();
        for (News news1 : news.stream().limit(2).collect(Collectors.toList())) {
            content.append(getHtmlNews(news1));
            content.append("<br>");
        }
        mailService.sendHtmlMail(
                "muyu订阅推送",
                content.toString(),
                subscribers.stream().map(MailSubscriber::getMailSubscriberAddr).toArray(String[]::new));
        for (MailSubscriber subscriber : subscribers) {
            for (News news1 : news) {
                recordsMapper.insert(MailSendRecord.builder()
                        .mailSender(subscriber.getId())
                        .sendTime(new Date())
                        .newsId(news1.getId())
                        .build());
            }
        }
    }

    public String getHtmlNews(News news){
        StringBuffer newsStr = new StringBuffer();
        newsStr.append("    <div>" +
                "           <p>" +
                "           <a href=\"" );
        newsStr.append(news.getNewsHref());
        newsStr.append("\"><img src=\""+localhost+"\"/img");
//        newsStr.append("\"><img src=\"http://localhost/img/");
        newsStr.append(news.getCover());
        newsStr.append("\" style=\"height: 100px;width: 100px\"\"></a>" +
                "          </p>");

        newsStr.append("  <font>\n" +
                "            <span>\n" +
                "             <a href=\"");
        newsStr.append(news.getNewsHref());
        newsStr.append("\">");
        newsStr.append(news.getNewsTitle());
        newsStr.append("</a>\n" +
                "              <br>\n" +
                "            <font>");
        newsStr.append(new SimpleDateFormat().format(news.getNewsDate()));

        newsStr.append("</font>\n" +
                "            </span>\n" +
                "          </font>\n" +
                "          <br>\n" +
                "         <font>");
        newsStr.append(news.getDescription());
        newsStr.append("</font>\n" +
                "        </div>");
        return newsStr.toString();
    }
}
